package com.zhaoyh.framework.taglib;


import com.zhaoyh.framework.code.model.PageBean;
import com.zhaoyh.framework.code.util.LogUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

/**
 * <p>
 * Title: 信息管理系统通用平台
 * </p>
 * <p>
 * Description: Information Management System Universal Platform
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: www.pingtech.com.cn
 * </p>
 * 
 * @author cmore
 * @version 1.0
 */

public class PageBeanTag extends BodyTagSupport {

	private String ctx = null;
	private String theme = null;
	private String front = null;
	private String confirm = "document.all('turnPage').click();";
	private String beanName = null;

	public PageBeanTag() {
	}

	public void setCtx(String ctx) {
		this.ctx = ctx;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public void setFront(String front) {
		this.front = front;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			if (this.beanName == null) {
				this.beanName = "pageBean";
			}
			PageBean pageBean = (PageBean) pageContext.getRequest().getAttribute(this.beanName);
			if (pageBean == null) {
				return super.doEndTag();
			}
			if (pageBean.getDataSum() == 0) {
				pageContext.getOut().print("<center style=\"text-color:#777777\">未找到符合条件的数据！</center>");
				return super.doEndTag();
			}
			StringBuffer bfString = new StringBuffer("");
			bfString.append("");
			if (this.front != null && this.front.equals("true")) { // 前台
				bfString.append("<input type=\"submit\" style=\"display:none\" name=\"turnPage\" >");
				bfString.append("共&nbsp;<span class=\"txt_red\">" + pageBean.getPageSum() + "</span>&nbsp;页");
				bfString.append("&nbsp;&nbsp;");
				bfString.append("<span class=\"txt_red\">" + pageBean.getDataSum() + "</span>条记录&nbsp;");
				bfString.append("&nbsp;&nbsp;");
				bfString.append("当前第&nbsp;<span class=\"txt_red\">" + pageBean.getPageNumber() + "</span>&nbsp;页");
				bfString.append("&nbsp;&nbsp;");
				if (pageBean.disabledFirstPage()) {
					bfString.append("首页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='1';"
							+ confirm + "\">首页</a>");
				}
				bfString.append("&nbsp;&nbsp;");
				if (pageBean.disabledPreviousPage()) {
					bfString.append("上一页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='"
							+ pageBean.getPreviousNumber() + "';" + confirm + "\">上一页</a>");
				}
				bfString.append("&nbsp;&nbsp;");
				if (pageBean.disabledNextPage()) {
					bfString.append("下一页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='"
							+ pageBean.getNextNumber() + "';" + confirm + "\">下一页</a>");
				}
				bfString.append("&nbsp;&nbsp;");
				if (pageBean.disabledLastPage()) {
					bfString.append("尾页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='"
							+ pageBean.getPageSum() + "';" + confirm + "\">尾页</a>");
				}
				bfString.append("&nbsp;&nbsp;");
				bfString.append("每页<input type=\"text\" id=\"pageSize\" name=\"" + this.beanName
						+ ".customPageSize\" maxlength=\"6\" size=\"3\" " + "value=\"" + pageBean.getPageSize() + "\" "
						+ "onkeypress=\"javascript:if (window.event.keyCode==13){" + confirm + " return false;}"
						+ "else if(window.event.keyCode<48||window.event.keyCode>57){return false;}\" "
						+ "onmouseover=\"javascript:this.select()\">条");
				bfString.append("&nbsp;&nbsp;");
				bfString.append("转到第");
				bfString.append("<input type=\"text\"");
				bfString.append(
						" id=\"pageNumber\" name=\"" + this.beanName + ".pageNumber\" size=\"7\" maxlength=\"7\"");
				bfString.append(" value=\"" + pageBean.getPageNumber() + "\"");
				bfString.append(
						" onkeypress=\"javascript:if (window.event.keyCode==13){document.all.pageBeanGo.click();"
								+ "return false;}else if(window.event.keyCode<48||window.event.keyCode>57){return false;}\"");
				bfString.append(" onmouseover=\"javascript:this.select()\"");
				bfString.append("" + (pageBean.disabledTurnTo() ? " disabled" : "") + ">");
				bfString.append("页&nbsp;");
				bfString.append("<input type=\"button\" class=\"bt2\" id=\"pageBeanGo\" onclick=\"" + confirm
						+ "\" value=\"跳转\">");

			} else {// 后台
				bfString.append("<input type=\"submit\" style=\"display:none\" name=\"turnPage\" >");
				bfString.append(pageBean.getInfoDataSum());
				bfString.append("&nbsp;&nbsp;");
				bfString.append("&nbsp;&nbsp;");
				if (pageBean.disabledFirstPage()) {
					bfString.append("首页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" "
							+ "onclick=\"document.all('pageNumber').value='1';" + confirm + "\">首页</a>");
				}
				bfString.append("&nbsp;|&nbsp;");
				if (pageBean.disabledPreviousPage()) {
					bfString.append("上一页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='"
							+ pageBean.getPreviousNumber() + "';" + confirm + "\">上一页</a>");
				}
				bfString.append("&nbsp;|&nbsp;");
				if (pageBean.disabledNextPage()) {
					bfString.append("下一页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='"
							+ pageBean.getNextNumber() + "';" + confirm + "\">下一页</a>");
				}
				bfString.append("&nbsp;|&nbsp;");
				if (pageBean.disabledLastPage()) {
					bfString.append("末页");
				} else {
					bfString.append("<a href=\"#\" hidefocus=\"true\" onclick=\"document.all('pageNumber').value='"
							+ pageBean.getPageSum() + "';" + confirm + "\">末页</a>");
				}
				bfString.append("&nbsp;&nbsp;");
				bfString.append("&nbsp;&nbsp;");
				bfString.append("每页<input type=\"text\" id=\"pageSize\" name=\"" + this.beanName
						+ ".customPageSize\" maxlength=\"3\" size=\"3\" " + "value=\"" + pageBean.getPageSize() + "\" "
						+ "onkeypress=\"javascript:if (window.event.keyCode==13){" + confirm + " return false;}"
						+ "else if(window.event.keyCode<48||window.event.keyCode>57){return false;}\" "
						+ "onmouseover=\"javascript:this.select()\">条");
				bfString.append("&nbsp;&nbsp;");
				bfString.append("&nbsp;&nbsp;");
				bfString.append("转到第");
				bfString.append("<input type=\"text\"");
				bfString.append(
						" id=\"pageNumber\" name=\"" + this.beanName + ".pageNumber\" size=\"7\" maxlength=\"7\"");
				bfString.append(" value=\"" + pageBean.getPageNumber() + "\"");
				bfString.append(
						" onkeypress=\"javascript:if (window.event.keyCode==13){document.all.pageBeanGo.click();"
								+ "return false;}else if(window.event.keyCode<48||window.event.keyCode>57){return false;}\"");
				bfString.append(" onmouseover=\"javascript:this.select()\"");
				bfString.append("" + (pageBean.disabledTurnTo() ? " disabled" : "") + ">");
				bfString.append("/" + pageBean.getPageSum() + "页&nbsp;");
				// bfString.append("<img id=\"pageBeanGo\" src=\"" + this.ctx +
				// "/theme/" + this.theme + "/images/001_21.gif\" onclick=\"" +
				// confirm + "\" border=\"0\" align=\"absmiddle\"
				// style=\"cursor:hand;\" width=\"16\" height=\"16\">");
			}
			pageContext.getOut().print(bfString.toString());
		} catch (IOException e) {
			LogUtil.log.error(e.getMessage(), e);
		}
		return super.doEndTag();
	}
}
